package com.github.cooingandwooing.process;

import java.util.List;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import com.github.cooingandwooing.model.CaseInfoDto;
import com.github.cooingandwooing.model.HuayuShouyeResponse;
import com.github.cooingandwooing.model.PageInfo;
import com.github.cooingandwooing.utils.HuayuUrlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import org.springframework.stereotype.Component;

import static org.apache.http.Consts.UTF_8;

/**
 * 〈GitHubProcessor 〉
 *
 * @author Administrator
 * @since 1.0.0
 */
@Slf4j
@Component
public class HuayuOneProcessor implements PageProcessor {
    /**
     * 列表页就抓取 url 如果详情页就抓取 案件信息
     * @param page
     */
    @Override
    public void process(Page page) {

        String pageRequestUrl = page.getUrl().toString();
        if (StringUtils.isNotBlank(pageRequestUrl)) {
            // 如果请求是详情页 则获取案件信息 并保存 page.putField("", )
            if (StringUtils.startsWith(pageRequestUrl, "http://132.36.1.7/ajxq/ajxq.do?action=queryMainTable")) {
                log.info("开始收集案件信息");
                List<CaseInfoDto> caseInfoDtoList = page.getJson().toList(CaseInfoDto.class);
                page.putField("caseInfoDtoList", caseInfoDtoList);
                // 获取案件 编码和类别

                UrlQuery urlQuery = UrlBuilder.of(pageRequestUrl, UTF_8).getQuery();
                String cBhAj = urlQuery.get("cBhAj").toString();
                String nAjlb = urlQuery.get("nAjlb").toString();
                page.putField("cBhAj", cBhAj);
                page.putField("nAjlb", nAjlb);
            }
        }
    }

    @Override
    public Site getSite() {
        // 如何解析页面
        return Site.me()
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 " + "Firefox/65.0")
                .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Accept-Encoding", "gzip, deflate").addHeader("Connection", "keep-alive")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
                .setCharset("utf-8") // 设置编码gbk
                .setTimeOut(60 * 1000) // 设置超时时间
                .setRetrySleepTime(3000) // 设置重试的间隔时间
                .setRetryTimes(3); // 设置重试的次数
    }
}